package com.wbcs.config.database.type;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.component.application.report.ReportDataSetBean;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.assistant.ReportAssistant;
import com.wbcs.system.datatype.BigdecimalType;
import com.wbcs.system.datatype.BlobType;
import com.wbcs.system.datatype.ClobType;
import com.wbcs.system.datatype.DateType;
import com.wbcs.system.datatype.DoubleType;
import com.wbcs.system.datatype.FloatType;
import com.wbcs.system.datatype.IDataType;
import com.wbcs.system.datatype.IntType;
import com.wbcs.system.datatype.LongType;
import com.wbcs.system.datatype.ShortType;
import com.wbcs.system.datatype.TimeType;
import com.wbcs.system.datatype.TimestampType;
import com.wbcs.system.datatype.VarcharType;
import com.wbcs.util.Tools;

public class Postgresql extends AbsDatabaseType
{
    private static Log log=LogFactory.getLog(Postgresql.class);

    @Override
	public String constructSplitPageSql(ReportDataSetBean svbean)
    {

        String sql=svbean.getSqlWithoutOrderby();
        if(sql.indexOf("%orderby%")>0)
        {
            sql=Tools.replaceAll(sql,"%orderby%"," order by "+svbean.getOrderby());
        }
        sql=sql+"   limit  %PAGESIZE%   OFFSET   %START% ";
        return sql;
    }

    @Override
	public String constructSplitPageSql(ReportDataSetBean svbean,String dynorderby)
    {
        dynorderby=ReportAssistant.getInstance().mixDynorderbyAndRowgroupCols(svbean.getReportBean(),dynorderby);
        dynorderby=" order by "+dynorderby;
        String sql=svbean.getSqlWithoutOrderby();
        if(sql.indexOf("%orderby%")<0)
        {
            sql=sql+dynorderby;
        }else
        {
            sql=Tools.replaceAll(sql,"%orderby%",dynorderby);
        }
        sql=sql+"   limit  %PAGESIZE%   OFFSET   %START% ";
        return sql;
    }

    @Override
	public String getSequenceValueByName(String sequencename)
    {
        log.warn("Postgresql数据库不支持序列的配置");
        return "";
    }
    
    @Override
	public String getSequenceValueSql(String sequencename)
    {
       throw new WbcsRuntimeException("Postgresql数据库不支持序列");
    }
    
    @Override
	public IDataType getWbcsDataTypeByColumnType(String columntype)
    {
        if(columntype==null||columntype.trim().equals("")) return null;
        columntype=columntype.toLowerCase().trim();
        IDataType dataTypeObj=null;
        if(columntype.equals("varchar")||columntype.equals("char"))
        {
            dataTypeObj=new VarcharType();

        }else if(columntype.equals("smallint"))
        {
            dataTypeObj=new ShortType();
        }else if(columntype.equals("int")||columntype.equals("integer")||columntype.equals("serial"))
        {
            dataTypeObj=new IntType();
        }else if(columntype.equals("bigint"))
        {
            dataTypeObj=new LongType();
        }else if(columntype.equals("bytea"))
        {
            dataTypeObj=new BlobType();
        }else if(columntype.equals("date"))
        {
            dataTypeObj=new DateType();
        }else if(columntype.equals("time"))
        {
            dataTypeObj=new TimeType();
        }else if(columntype.equals("decimal"))
        {
            dataTypeObj=new BigdecimalType();
        }else if(columntype.equals("real"))
        {
            dataTypeObj=new FloatType();
        }else if(columntype.equals("double"))
        {
            dataTypeObj=new DoubleType();
        }else if(columntype.equals("timestamp"))
        {
            dataTypeObj=new TimestampType();
        }else if(columntype.equals("text"))
        {
            dataTypeObj=new ClobType();
        }else
        {
            log.warn("数据类型："+columntype+"不支持，将当做varchar类型");
            dataTypeObj=new VarcharType();
        }
        return dataTypeObj;
    }
}
